import hotReload from '@/utils/hotReload';
const resolveData = value => {
  let str = value.toString();
  if(str.indexOf('.') !== -1){
    str = str.replace('.','')
  }
  if (str.length < 13) {
    for(var i=0;i<13-str.length;i++){
      str = str + '0'
    }
    return str
  }else {
    return str.substr (0, 13);
  }
};
hotReload ();
// 监听来自content-script的消息
chrome.runtime.onMessage.addListener (function (request, sender, sendResponse) {
  console.log('request:' + JSON.stringify(request))
  chrome.tabs.query (
    {
      active: true,
      currentWindow: true,
    },
    tabs => {
      console.log (tabs);
      let destUrl = '';
      try {
        destUrl = tabs[0].url;
      } catch (err) {
        console.log (err.message);
      }
      if (destUrl !== '') {
        chrome.cookies.getAll (
          {
            //domain: destUrl.split ('/')[2].split (':')[0] || '',
            domain: 'baidu.com'
          },
          cookies => {
            console.log ('cookie:' + cookies);
            const cookieArr = [];
            cookies.map (cookie => {
              if (cookie.expirationDate) {
                cookieArr.push ({
                  ...cookie,
                  expirationDate: resolveData (cookie.expirationDate),
                });
              } else {
                cookieArr.push ({
                  ...cookie,
                });
              }
            });
            const fileInfo = {
              cookie: JSON.stringify (cookieArr),
              pageUrl: destUrl,
            };
            console.log('response:' + JSON.stringify(fileInfo))
            if (sendResponse && typeof sendResponse === 'function') {
              sendResponse (fileInfo);
            }
          }
        );
      }
    }
  );
  return true;
});
